package com.jstyle.jclifexd.adapter;

import android.animation.ValueAnimator;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.utils.DateUtil;
import com.jstyle.jclifexd.utils.ScreenUtils;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/8/8.
 */

public class HistoryHeartRangeAdapter extends RecyclerViewBaseAdapter {


    private int[] data;
    private float total;
    private int totalWidth;

    public HistoryHeartRangeAdapter(int[] data) {
        super(data);
        this.data = data;
    }

    public HistoryHeartRangeAdapter(Context context, int[] data, int total) {
        super(data);
        this.data = data;
        this.total = total;
        totalWidth = ScreenUtils.dip2px(context, 180);
    }
    ValueAnimator animator;
    @Override
    protected void bindData(BaseViewHolder holder, int position) {
        final HeartRangeViewHolder viewHolder = (HeartRangeViewHolder) holder;
        Context context = viewHolder.itemView.getContext();
        int id = context.getResources().getIdentifier("heart_progressbar_" + position,
                "drawable", context.getPackageName());
          int progress = data[position];

        viewHolder.tvHeartName.setText(viewHolder.heartArray[position]);
        viewHolder.tvHeartTime.setText(DateUtil.getSportAxieTime(progress));
        viewHolder.progressBarHeartRange.setProgressDrawable(context.getResources().getDrawable(id));
        if (data[position] != 0) {
           animator = ValueAnimator.ofInt(0, data[position]).setDuration(1200);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    ConstraintSet constraintSet = new ConstraintSet();
                    constraintSet.clone(viewHolder.ConstraintLayoutHeartRange);
                    constraintSet.constrainWidth(R.id.progressBar_heartRange, (int) ((int) valueAnimator.getAnimatedValue() / total * totalWidth));
                    constraintSet.applyTo(viewHolder.ConstraintLayoutHeartRange);
                }
            });
            animator.start();
        }
        //viewHolder.progressBarHeartRange.
    }




    @Override
    public int getItemCount() {
        return 5;
    }

    class HeartRangeViewHolder extends BaseViewHolder {
        @BindView(R.id.progressBar_heartRange)
        ProgressBar progressBarHeartRange;
        @BindView(R.id.tv_heart_time)
        TextView tvHeartTime;
        @BindView(R.id.tv_heart_name)
        TextView tvHeartName;
        @BindView(R.id.ConstraintLayout_heartRange)
        ConstraintLayout ConstraintLayoutHeartRange;
        @BindArray(R.array.heartRange_array)
        String[] heartArray;

        public HeartRangeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    protected int getLayoutId(int viewType) {
        return R.layout.item_heartrange;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new HeartRangeViewHolder(getInflaterView(parent, viewType));
    }
}
